<?php
namespace app\api\controller;
use app\Response;
use QL\QueryList;
use \Curl\Curl;
use think\Db;

class Rename 
{	//修改章节，由“章二”改为“第二章”
	public function index(){
		$basedir = dirname(__FILE__);
		$file = $basedir.'\1.txt';
		$content = file_get_contents($file);

    		//$arr = self::getCharpos2($content, '章');
    		$arr = explode("\r\n", $content);

    		$num_arr = array('零','一','二','三','四','五','六','七','八','九','0','1','2','3','4','5','6','7','8','9');
    		$chapter_key = array('第','章','节','卷');

    		foreach ($arr as $key => $val) {
    			if(self::hasstring($val,'章')){
    				//含有“章”字的行去除空格，逐字拆解匹配数字，直到没有数字
    				$val = str_ireplace('章', '', $val);
    				$val = str_ireplace(' ', '', $val);
    				$str_arr = self::ch2arr($val);
    				$str = '';
    				
    				if($str_arr){
    					$str .= '第';
    					//判断数据中是否有数字
    					$has_num = false;

    					$new_arr = $str_arr;
    					foreach ($str_arr as $k => $v) {
    						//有数字，将$has_num置为true
    						if(in_array($v, $num_arr) && $k==0){
    							$has_num = true;
    						}
    						//有数字，将数字部分抛出数组
    						if(in_array($v, $num_arr)){						
    							$str .= $v;
    							$new_arr = array_merge(array_diff($new_arr, array($v)));
    						}
    						//如果有数字，到没有数字时截断，插入空格
    						if($has_num && !in_array($v, $num_arr)){
    							$str .= '章';
    							break;
    						}
    					}
    					//有数字，有“章”的才算目录
    					if($has_num){
    						$str .= ' '.implode('', $new_arr);
    						$arr[$key] = $str;
    					}
    				}

    				/*$kongge_arr = array();
    				$kongge_arr = explode(' ', $val);
    				$a = false;

    				foreach ($kongge_arr as $m => $o) {
    					if(self::hasstring($o,'章')){
    						$value = '';
		    				$value = str_ireplace('章', '', $o);
		    				$urlen = urlencode($value); 
		    				if($value && !preg_match("/(%7E|%60|%21|%40|%23|%24|%25|%5E|%26|%27|%2A|%28|%29|%2B|%7C|%5C|%3D|\-|_|%5B|%5D|%7D|%7B|%3B|%22|%3A|%3F|%3E|%3C|%2C|\.|%2F|%A3%BF|%A1%B7|%A1%B6|%A1%A2|%A1%A3|%A3%AC|%7D|%A1%B0|%A3%BA|%A3%BB|%A1%AE|%A1%AF|%A1%B1|%A3%FC|%A3%BD|%A1%AA|%A3%A9|%A3%A8|%A1%AD|%A3%A4|%A1%A4|%A3%A1|%E3%80%82|%EF%BC%81|%EF%BC%8C|%EF%BC%9B|%EF%BC%9F|%EF%BC%9A|%E3%80%81|%E2%80%A6%E2%80%A6|%E2%80%9D|%E2%80%9C|%E2%80%98|%E2%80%99|%EF%BD%9E|%EF%BC%8E|%EF%BC%88)+/",$urlen)){
		    					$kongge_arr[$m] = '第'.$value.'章';
		    					$a = true;
		    				}
    					}
    					

    				}
    				$kongge_str = implode(' ', $kongge_arr);
    				
				$arr[$key] = $kongge_str;*/
    			}
    		}
    		$content_str = implode("\r\n", $arr);
    		
    		file_put_contents($basedir.'\2.txt',$content_str);
    		Response::show(200,'ok');
	}
    /**
     * [numberToChapter 数字转为汉字]
     * @param  [int] $number [阿拉伯数字]
     * @return [string]         [中文汉字]
     */
    public static function numberToChapter($number){
        $number = (int)$number;
        $arr = self::ch2arr($number);
        $length = count($arr);

        $num_arr = array('零','一','二','三','四','五','六','七','八','九');

        $wei = array('万', '千', '百', '十', '');   //因为有零，所以个位数置为空
        $wei_start_key = 4;
        if($length>1){
            switch ($length) {
                case 5:
                    $wei_start_key = 0;   //万位数 开始的key值
                    break;

                case 4:
                    $wei_start_key = 1;   //千位数
                    break;
                
                case 3:
                    $wei_start_key = 2;   //百位数
                    break;

                case 2:
                    $wei_start_key = 3;   //十位数
                    break;                
                
                default:
                    $wei_start_key = 4;   //个位数
                    break;
            }
        }
        //两种模式，尾数为零和不为零,type:1尾数为0,type:2尾数不为0
        $type = ($arr[$length-1] == 0)?1:2;
        $before_ling_key = $length-1;   //倒叙与个位相连同为零的最大key值
        if($type == 1){
            //尾数为0,从末尾开始倒叙同为0的省去转化，到不为0截止
            for ($i=$length-1; $i >= 0 ; $i--) { 
                $before_ling_key = $i;
                if($arr[$i] > 0){
                    break;
                }
            }         
        }

        $name = '';
        $last_val = '';
        foreach ($arr as $key => $val) {
            //1301 一千三百零一    100 一百   10 十  1020 一千零二十  1001 一千零一 
            
            //对10~19做特殊处理，如一十一，我们通常直接用十一
            if($length == 2 && $key == 0 && $val == 1){
                $name .= isset($num_arr[$val])?$wei[$wei_start_key]:'';
            }else if($type == 1 && $key <= $before_ling_key){
                //尾数为0,从末尾开始倒叙同为0的省去转化，到不为0截止
                //判断此时的值是否为零，不为零加上单位，为零不加
                if($val != 0){
                    $name .= isset($num_arr[$val])?($num_arr[$val] . $wei[$wei_start_key]):'';
                }else if($last_val != 0){
                    $name .= isset($num_arr[$val])?$num_arr[$val]:'';
                }
            }else if($type == 2){
                //尾数不为0
                //判断此时的值是否为零，不为零加上单位，为零不加
                if($val != 0){
                    $name .= isset($num_arr[$val])?($num_arr[$val] . $wei[$wei_start_key]):'';
                }else if($last_val != 0){
                    $name .= isset($num_arr[$val])?$num_arr[$val]:'';
                }                
            }
            $wei_start_key++;
            $last_val = $val;  //上一位的数值，为了处理连续两个0的情况，如1002
        }
        return $name;
    }

	/**
	 * 查找某个字符在字符串中出现的所有位置
	 * $str string 被查询字符串
	 * $char string 查询字符
	 * $arr  array() 返回数组
	 */
	public static function getCharpos2($str, $char){
	       $j = 0;
	       $arr = array();
	       $count = substr_count($str, $char);
	       for($i = 0; $i < $count; $i++){
	             $j = strpos($str, $char, $j);
	             $arr[] = $j;
	             $j = $j+1;
	       }
	       return $arr;
	}

	 /**
	 * 判断字符串中是否包含指定字符串
	 * @var source 源字符串
	 * @var target 要判断的是否包含的字符串
	 * @return bool
	 */
	public static function hasstring($source,$target){
	    preg_match_all("/$target/sim", $source, $strResult, PREG_PATTERN_ORDER);
	    return !empty($strResult[0]);
	}
	/**
	 * [ch2arr 中文字符串拆解]
	 * @param  [type] $str [字符串]
	 * @return [type]      [description]
	 */
	public static function ch2arr($str)
	{
	    	$length = mb_strlen($str, 'utf-8');
	    	$array = [];
	    	for ($i=0; $i<$length; $i++)  
	        		$array[] = mb_substr($str, $i, 1, 'utf-8');    
	    	return $array;
	}

}